package action.order;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import model.MWxMessage;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import action.common.MyBaseAction;
import com.opensymphony.xwork2.ModelDriven;
import common.impl.MicroBlog;
import common.wx.WxConfig;
import common.wx.WxJsAPI;

public class JsApiAction extends MyBaseAction<MWxMessage> implements
		ModelDriven<MWxMessage> {

	private static final long serialVersionUID = 1L;
	private String url;
	private String signType;
	private String prepay_id;

	public void setUrl(String url) {
		this.url = url;
	}
	
	public void setSignType(String signType) {
		this.signType = signType;
	}
	
	public void setPrepay_id(String prepayId) {
		prepay_id = prepayId;
	}

	public MWxMessage getModel() {
		return model;
	}

	public JsApiAction() {
		super(MWxMessage.class);
	}

	public void getticket() {
		String r = "1";
		try {
			r = WxJsAPI.getConfig(url, signType);
		} catch (Exception e) {
			e.printStackTrace();
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	public void sign() {
		String r = "1";
		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("appId", WxConfig.wx_appid);
			String timeStamp = String.valueOf(new Date().getTime());
			params.put("timeStamp", timeStamp);
			String nonce_str = UUID.randomUUID().toString().replaceAll("-", "");
			params.put("nonceStr", nonce_str);
			params.put("package", "prepay_id=" + prepay_id);
			params.put("signType", "MD5");
			String sign = MicroBlog.sign(params, WxConfig.wx_key);
			JSONObject jo = new JSONObject();
			jo.put("appId", WxConfig.wx_appid);
			jo.put("timeStamp", timeStamp);
			jo.put("nonceStr", nonce_str);
			jo.put("signature", sign);
			r = jo.toString();
		} catch (Exception e) {
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

}
